/*
** StopWatch
**
** Encapsulates calls to the performance timers
**		StopWatch::Instance()->GetDeltaT() returns the amount
**		of time (in seconds) since it was Last called
**
** Revision: %Id$
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "StopWatch.h"


/*****************************************************\
* Instance returns pointer to the singleton instance  *
\*****************************************************/
StopWatch::StopWatch()
{
	__int64 countsPerSecond = 0;

	if (!QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSecond))
	{
		throw "Error: no time base!";
	}
	secondsPerCount = 1 / (double)countsPerSecond;
	QueryPerformanceCounter((LARGE_INTEGER*)&then);
}


/*****************************************************\
* Instance returns pointer to the singleton instance  *
\*****************************************************/
StopWatch* StopWatch::Instance()
{
	static StopWatch instance;
	return &instance;
}


/*****************************************************\
* Instance returns pointer to the singleton instance  *
\*****************************************************/
StopWatch::~StopWatch()
{
}


double StopWatch::GetDeltaT()
{
	__int64 now = 0;

	QueryPerformanceCounter((LARGE_INTEGER*)&now);
	double retVal = (now - then) * secondsPerCount;
	then = now;

	return retVal;
}


void StopWatch::Reset()
{
	QueryPerformanceCounter((LARGE_INTEGER*)&then);
}
